﻿CREATE PROCEDURE [dbo].[AddEntityProperty]
	@entity_id int,
	@entity_type_property_id int,
	@string_value varchar(256),
	@base_type varchar(128),
	@int_value int = null,
	@double_value float = null,
	@datetime_value datetime = null,
	@bigint_value bigint = null
AS
declare @result int;
if exists(select entity_property_id from dbo.EntityProperty where entity_id = @entity_id and entity_type_property_id = @entity_type_property_id)
begin
	select @result = entity_property_id from dbo.EntityProperty where entity_id = @entity_id and entity_type_property_id = @entity_type_property_id;
	update dbo.EntityProperty set 
		string_value = @string_value,
		int_value = @int_value,
		double_value = @double_value,
		datetime_value = @datetime_value
	where 
		entity_id = @entity_id and
		entity_type_property_id = @entity_type_property_id;
end
else
begin
	insert into dbo.EntityProperty
		(entity_id, entity_type_property_id, string_value, base_type, int_value, double_value, datetime_value, bigint_value)
		values
		(@entity_id, @entity_type_property_id, @string_value, @base_type, @int_value, @double_value, @datetime_value, @bigint_value);
	select @result = SCOPE_IDENTITY();
end
select @result;